$(function(){
    let form = layui.form
    let q = {
        pagenum:1,
        pagesize:10,
        cate_id:'',
        state:'',

    }
    //渲染下来选框
    initCateList()
    function initCateList() {
        axios({
            url:'/my/article/cates',
            method:'get',
           
        }).then(({data:res})=>{
            //成功回调
            console.log(res)
            if(res.status != 0 ) return layer.msg(res.message)
            let arr = ['<option value="">请选择分类</option>']

            res.data.forEach((value ,index) => {
                arr.push(`<option value="${value.Id}">${value.name}</option>`)
            });
            console.log(arr)
            $('[name=cate_id]').empty().html(arr.join(''))
            form.render()
        });
    }

    //获取文章列表
    initArticleList()
    function initArticleList() {
        axios({
            url:'/my/article/list',
            method:'get',
            params:q
        }).then(({data:res})=>{
            //成功回调
            if(res.status != 0) return layer.msg(res.message)
            
            //渲染table 
            let arr = []
            res.data.forEach((value,index)=>{
                arr.push(`<tr>
                            <td>${value.title}</td>
                            <td>${value.cate_name}</td>
                            <td>${value.pub_date}</td>
                            <td>${value.state}</td>
                            <td>
                                <button class="layui-btn layui-btn-xs">修改</button>
                                <button class="layui-btn layui-btn-danger layui-btn-xs">删除</button></td>
                            <tr>
                `)
            })
            $('tbody').empty().html(arr.join(''))
            renderPage(res.total)
        });

        //提交表单 
        $('form').on('submit',function(e){
            e.preventDefault()
            q.cate_id = $('[name=cate_id]').val()
            q.state = $('[name=state]').val()

            initArticleList()
        })

        function renderPage(total) {
            var  laypage = layui.laypage

            laypage.render({
                elem:'pageBox',
                count:total,
                limit:q.pagesize,
                curr:q.pagenum,
                jump:function (obj , first) {
                    if(!first){
                        q.pagenum = obj.curr
                        q.pagesize = obj.limit
                        initArticleList()
                    }
                }
            })
        }


    }











})